// pages/hospital/hospital.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    imgs: [],
    interview_time: '',
    person_id: '',
    tempImg: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      person_id: options.person_id
    })
  },

  // 释解日期
  timeChange(e) {
    this.setData({
      interview_time: e.detail.value
    })
  },

  // 图片操作的具体函数
  ImageOperator() {
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        console.log(res)
        // 上传的图片数据
        let imgList = res.tempFilePaths;
        // 原始的图片数据
        let imageList = this.data.images;
        // 原来的图片数量
        let imageLength = imageList.length;
        // 当前的图片数量
        let nowLength = imgList.length;
        if (imageLength < 5) {
          let images = [];
          // 获取缺少的图片张数
          let residue = 5 - imageLength;
          // 如果缺少的张数大于当前的的张数  
          if (residue >= nowLength) {
            // 直接将两个数组合并为一个  
            images = imageList.concat(imgList);
          } else {
            // 否则截取当前的数组一部分  
            images = imageList.concat(imgList.slice(0, residue));
          }
          this.setData({
            images,
          })
        }
      }
    })
  },
  // 图片获取
  chooseImage() {
    this.ImageOperator()
  },
  // 删除图片
  deleteImage(event) {
    //获取数据绑定的data-id的数据
    const nowIndex = event.currentTarget.dataset.id;
    let images = this.data.images;
    images.splice(nowIndex, 1);
    this.setData({
      images
    })
  },
  // 预览图片
  previewIamge(event) {
    const nowIndex = event.currentTarget.dataset.id;
    const images = this.data.images;
    wx.previewImage({
      current: images[nowIndex],  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },

  formSubmit(e) {
    wx.showLoading({
      title: '加载中...',
    })
    let access_token = wx.getStorageSync('access_token')
    let { interview_time, interviewer, content } = e.detail.value
    let person_id = this.data.person_id
    // promise上传表单
    let promiseArr = []
    let uploadArr = []
    for (var i = 0; i < this.data.images.length; i++) {
      let promise = new Promise((resolve, reject) => {
        wx.uploadFile({
          filePath: this.data.images[i],
          name: 'file',
          url: 'https://baokang.wshoto.com/wx/upload',
          complete: (res) => {
            let resArr = JSON.parse(res.data).url
            uploadArr.push(resArr)
            this.setData({
              imgs: uploadArr
            })
            resolve(res)
          }
        })
      })
      promiseArr.push(promise)
    }
    Promise.all(promiseArr).then(res => {
      let pic = this.data.imgs.join(';')
      wx.request({
        url: 'https://baokang.wshoto.com/wx/interviewadd',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          access_token: access_token,
          person_id: person_id,
          interview_time: interview_time,
          interviewer: interviewer,
          content: content,
          pic: pic
        },
        success: (res) => {
          console.log(res)
          wx.hideLoading()
          if (res.data.code == 1) {
            wx.showModal({
              title: '提示',
              content: '提交成功',
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
            })
          }
        }
      })
    })
  }
})